package com.example.administrator.waitan.homepage.recommend;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.waitan.R;
import com.example.administrator.waitan.tools.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Administrator on 2016/1/25.
 */
public class RecommendBaseAdapter extends BaseAdapter {
    private Context context;
    private List<RecommendInfo> data;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;
    final int TYPE_4 = 3;


    public RecommendBaseAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<RecommendInfo> data) {
        this.data = data;
        notifyDataSetChanged();
        Log.d("size", data.size() + "");
    }

    @Override
    public int getCount() {
        return data.size();

    }

    @Override
    public int getItemViewType(int position) {
        Integer Type = data.get(position).getDisplayMode();
        if (Type == 1) {
            return TYPE_1;
        } else if (Type == 2) {
            return TYPE_2;
        } else if (Type == 3) {
            return TYPE_3;
        } else {
            return TYPE_4;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyBigViewHolder myBigViewHolder = null;
        MySmallViewHolder mySmallViewHolder = null;
        MyMiddleViewHolder myMiddleViewHolder = null;
        MyMoreViewHolder myMoreViewHolder = null;
        int type = getItemViewType(i);
        if (view == null) {
            switch (type) {
                case TYPE_1:
                    view = LayoutInflater.from(context).inflate(R.layout.item_lv_recommend_small, null);
                    mySmallViewHolder = new MySmallViewHolder();
                    mySmallViewHolder.title = (TextView) view.findViewById(R.id.tv_item_recommend_small_title);
                    view.setTag(mySmallViewHolder);
                    break;
                case TYPE_2:
                    view = LayoutInflater.from(context).inflate(R.layout.item_lv_recommend_middle, null);
                    myMiddleViewHolder = new MyMiddleViewHolder();
                    myMiddleViewHolder.title = (TextView) view.findViewById(R.id.tv_item_commend_middle_title);
                    myMiddleViewHolder.content = (TextView) view.findViewById(R.id.tv_item_recommend_middle_content);
                    myMiddleViewHolder.pic = (ImageView) view.findViewById(R.id.iv_item_recommend_middle_pic);
                    view.setTag(myMiddleViewHolder);
                    break;
                case TYPE_3:
                    view = LayoutInflater.from(context).inflate(R.layout.item_lv_recommend_more, null);
                    myMoreViewHolder = new MyMoreViewHolder();
                    myMoreViewHolder.title = (TextView) view.findViewById(R.id.tv_item_recommend_more_title);
                    myMoreViewHolder.picOne = (ImageView) view.findViewById(R.id.iv_item_recommend_more_one);
                    myMoreViewHolder.picTwo = (ImageView) view.findViewById(R.id.iv_item_recommend_more_two);
                    myMoreViewHolder.picThree = (ImageView) view.findViewById(R.id.iv_item_recommend_more_three);
                    view.setTag(myMoreViewHolder);
                    break;
                case TYPE_4:
                    view = LayoutInflater.from(context).inflate(R.layout.item_lv_recommend_big, null);
                    myBigViewHolder = new MyBigViewHolder();
                    myBigViewHolder.title = (TextView) view.findViewById(R.id.tv_item_recommend_big_title);
                    myBigViewHolder.pic = (ImageView) view.findViewById(R.id.iv_item_recommend_big_pic);
                    view.setTag(myBigViewHolder);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    mySmallViewHolder = (MySmallViewHolder) view.getTag();
                    break;
                case TYPE_2:
                    myMiddleViewHolder = (MyMiddleViewHolder) view.getTag();
                    break;
                case TYPE_3:
                    myMoreViewHolder = (MyMoreViewHolder) view.getTag();
                    break;
                case TYPE_4:
                    myBigViewHolder = (MyBigViewHolder) view.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
                mySmallViewHolder.title.setText(data.get(i).getName());
                break;
            case TYPE_2:
                myMiddleViewHolder.title.setText(data.get(i).getName());
                myMiddleViewHolder.content.setText(data.get(i).getDescription());
                VolleySingleton.getImageLoader().get(data.get(i).getCoverUrl(), VolleySingleton.getImageListener(myMiddleViewHolder.pic));
                break;
            case TYPE_3:
                myMoreViewHolder.title.setText(data.get(i).getName());
                VolleySingleton.getImageLoader().get(data.get(i).getExtParam().getPicList().get(0),VolleySingleton.getImageListener(myMoreViewHolder.picOne));
                VolleySingleton.getImageLoader().get(data.get(i).getExtParam().getPicList().get(1), VolleySingleton.getImageListener(myMoreViewHolder.picTwo));
                VolleySingleton.getImageLoader().get(data.get(i).getExtParam().getPicList().get(2), VolleySingleton.getImageListener(myMoreViewHolder.picThree));
                break;
            case TYPE_4:
                myBigViewHolder.title.setText(data.get(i).getName());
                VolleySingleton.getImageLoader().get(data.get(i).getCoverUrl(), VolleySingleton.getImageListener(myBigViewHolder.pic));
                break;
        }
        return view;
    }

    class MySmallViewHolder {
        TextView title;
    }

    class MyMiddleViewHolder {
        TextView title, content;
        ImageView pic;
    }

    class MyBigViewHolder {
        TextView title;
        ImageView pic;
    }

    class MyMoreViewHolder {
        TextView title;
        ImageView picOne, picTwo, picThree;
    }

}
